package com.xiaoyu.lanling.feature.chat.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.dp
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseDialogFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.chat.model.relationship.Intimacy
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.media.image.UserImageLoadParam
import kotlinx.android.synthetic.main.chat_intimacy_dialog.*

class ChatIntimacyDialogFragment : BaseDialogFragment() {

    companion object {

        val TAG: String = ChatIntimacyDialogFragment::class.java.simpleName

        fun show(fragmentManager: FragmentManager, toUser: User, intimacy: Intimacy) {
            val params = Bundle()
            params.putSerializable(Constant.IntentKey.KEY_INTIMACY, intimacy)
            params.putSerializable(Constant.IntentKey.KEY_USER, toUser)

            val fragment = (fragmentManager.findFragmentByTag(TAG) as? BaseDialogFragment)
                    ?: ChatIntimacyDialogFragment()
            fragment.arguments = params
            fragment.show(fragmentManager, TAG)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreenDimStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.chat_intimacy_dialog, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initBind()
        initData()
    }

    private fun initBind() {
        close.setOnClickDebounceListener { dismiss() }
    }

    private fun initData() {
        val intimacy = (arguments?.getSerializable(Constant.IntentKey.KEY_INTIMACY) as? Intimacy)
                ?: return
        val toUser = (arguments?.getSerializable(Constant.IntentKey.KEY_USER) as? User) ?: return

        ImageViewUtil.loadImage(left_avatar, getUserAvatarParams(user = toUser))
        ImageViewUtil.loadImage(right_avatar, getUserAvatarParams(user = UserData.getInstance().user))
        intimacy_title.text = getString(R.string.chat_intimacy_dialog_title, intimacy.totalNum.toString())
        intimacy_rule_title.text = intimacy.ruleTitle
        intimacy_rule_desc.text = intimacy.ruleDesc
        intimacy_unlock_title.text = intimacy.unlockTitle
        intimacy_unlock_desc.text = intimacy.unlockDesc
    }

    private fun getUserAvatarParams(user: User): UserImageLoadParam? {
        return UserImageLoadParam.newBuilder()
                .setUserAndDPRound(user, 80)
                .setRounded(true)
                ?.setRoundingBorderColor(AppContext.getColor(R.color.chat_intimacy_accent))
                ?.setRoundingBorderWidth(2.dp.toInt())
                ?.build()
    }
}